package com.baizhi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author Wgy * @date 2020/9/8 16:07 * @describe 跨域配置类
 */
@Configuration
class MyCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();        //这里只做演示，故所有条件都允许，生产环境根据自己的要求自己配置
        corsConfiguration.addAllowedHeader("*");//允许所有的请求头跨域
        corsConfiguration.addAllowedMethod("*");//允许所有的请求方式跨域  如：get post put delete
        corsConfiguration.addAllowedOrigin("*");//允许所有的请求来源跨域
        corsConfiguration.setAllowCredentials(true);//允许携带cookies跨域
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
